﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GlobalGameJam
{
    public class SpriteManager
    {
        public enum State
        {
            RunLeft,
            RunRight,
            StandUp,
            StandUpLeft,
            BiteRight,
            BiteLeft,
            JumpRight,
            JumpLeft,
            FallRight,
            FallLeft
        };

        public Color[] data;
        public bool _isRight;
        public bool _isJump;
        public bool _isBite;
        public bool _globool = false;
        public List<Rectangle> _RunRight = new List<Rectangle>();
        public List<Rectangle> _RunLeft = new List<Rectangle>();
        public List<Rectangle> _StandUp = new List<Rectangle>();
        public List<Rectangle> _StandUpLeft = new List<Rectangle>();
        public List<Rectangle> _JumpRight = new List<Rectangle>();
        public List<Rectangle> _BiteRight = new List<Rectangle>();
        public List<Rectangle> _FallRight = new List<Rectangle>();
        public List<Rectangle> _BiteLeft = new List<Rectangle>();
        public List<Rectangle> _JumpLeft = new List<Rectangle>();
        public List<Rectangle> _FallLeft = new List<Rectangle>();
        public List<Rectangle> tmp;
        public State _state = State.RunLeft;
        public Texture2D _sprite;
        private Vector2 _spritepos = new Vector2(0, 0);
        private IEnumerator<Rectangle> _it;

        public void LoadContent(ContentManager theContentManager, string theAssetName)
        {
            _sprite = theContentManager.Load<Texture2D>(theAssetName);
            data = new Color[_sprite.Width * _sprite.Height];
            _sprite.GetData(data);
            _StandUpLeft.Add(new Rectangle(0, 75, 75, 75));
            _RunLeft.Add(new Rectangle(75, 75, 75, 75));
            _RunLeft.Add(new Rectangle(150, 75, 75, 75));
            _JumpLeft.Add(new Rectangle(225, 75, 75, 75));
            _JumpLeft.Add(new Rectangle(300, 75, 75, 75));
            _FallLeft.Add(new Rectangle(375, 75, 75, 75));
            _FallLeft.Add(new Rectangle(450, 75, 75, 75));
            _BiteLeft.Add(new Rectangle(525, 75, 75, 75));
            _BiteLeft.Add(new Rectangle(600, 75, 75, 75));

            _StandUp.Add(new Rectangle(0, 0, 75, 75));
            _RunRight.Add(new Rectangle(75, 0, 75, 75));
            _RunRight.Add(new Rectangle(150, 0, 75, 75));
            _JumpRight.Add(new Rectangle(225, 0, 75, 75));
            _JumpRight.Add(new Rectangle(300, 0, 75, 75));
            _FallRight.Add(new Rectangle(375, 0, 75, 75));
            _FallRight.Add(new Rectangle(450, 0, 75, 75));
            _BiteRight.Add(new Rectangle(525, 0, 75, 75));
            _BiteRight.Add(new Rectangle(600, 0, 75, 75));

            this.setState(State.RunLeft);
        }

        public Sprite getImage()
        {
            Sprite _image = new Sprite();
            _image.sprite = _sprite;
            _image.rec = _it.Current;
            return (_image);
        }

        public void nextFrame()
        {
            if (!_it.MoveNext())
            {
                _it = tmp.GetEnumerator();
                this.nextFrame();
            }
        }

        public State getState()
        {
            return _state;
        }

        public void setState(State _new)
        {
            if (_state != _new || _globool == false)
            {
                if (_new == State.RunLeft)
                {
                    tmp = _RunLeft;
                    _it = _RunLeft.GetEnumerator();
                }
                else if (_new == State.RunRight)
                {
                    tmp = _RunRight;
                    _it = _RunRight.GetEnumerator();
                }
                else if (_new == State.StandUp)
                {
                    tmp = _StandUp;
                    _it = _StandUp.GetEnumerator();
                }
                else if (_new == State.StandUpLeft)
                {
                    tmp = _StandUpLeft;
                    _it = _StandUpLeft.GetEnumerator();
                }
                else if (_new == State.JumpRight)
                {
                    tmp = _JumpRight;
                    _it = _JumpRight.GetEnumerator();
                }
                else if (_new == State.JumpLeft)
                {
                    tmp = _JumpLeft;
                    _it = _JumpLeft.GetEnumerator();
                }
                else if (_new == State.BiteRight)
                {
                    tmp = _BiteRight;
                    _it = _BiteRight.GetEnumerator();
                }
                else if (_new == State.BiteLeft)
                {
                    tmp = _BiteLeft;
                    _it = _BiteLeft.GetEnumerator();
                }
                else if (_new == State.FallRight)
                {
                    tmp = _FallRight;
                    _it = _FallRight.GetEnumerator();
                }
                else if (_new == State.FallLeft)
                {
                    tmp = _FallLeft;
                    _it = _FallLeft.GetEnumerator();
                }
                else
                    _it = null;
            }
            _globool = true;
            _state = _new;
            this.nextFrame();
        }
    }
}

